#include "ADLER32.h"

uint32_t ADLER32::adler32( unsigned char *data , int32_t len )
{
    uint32_t a = 1, b = 0;
    int32_t index;

    /* Process each byte of the data in order */
    for (index = 0; index < len; ++index)
    {
        a = (a + data[index]) % MOD_ADLER;
        b = (b + a) % MOD_ADLER;
    }

    return (b << 16) | a;
}

ADLER32::ADLER32( const std::string& text )
{
    unsigned char *cstr = new unsigned char[text.size()];
    memcpy( cstr , text.c_str() , text.size() );
    this->resultado = this->adler32( cstr , text.size() );
    delete[] cstr;
}

std::string ADLER32::hexdigest() const
{
    std::stringstream OS;
    OS << std::setw(8)  << std::setfill('0') << std::hex << (int) this->resultado;
    return OS.str() ;
}



